package com.qph.app.system.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.qph.app.system.dao.IRoleDao;
import com.qph.app.system.service.IRoleService;
import com.qph.base.security.entity.Permission;
import com.qph.base.security.entity.Role;
import com.qph.base.service.impl.BaseServiceImpl;
import com.qph.utils.base.StringUtil;


/**
 * 角色管理
 * @author Administrator
 *
 */
public class RoleServiceImpl extends BaseServiceImpl<Role, IRoleDao> implements
		IRoleService {

	@Transactional
	public int delete(String ids) {
		
		if (StringUtil.isEmpty(ids)){
			return 0; 
		}
		String[] idArr = ids.split(SPLIT_COMMA);
		List<String> list = new ArrayList<String>(Arrays.asList(idArr));
		// 删除角色下用户
		dao.delUsersByRoles(list);
		//删除角色下权限
		dao.delRolePerms(list);
		return dao.batchDelete(list);
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.qph.app.system.service.IRoleService#saveRoleUsers(java.lang.String, java.lang.String)
	 */
	public boolean saveRoleUsers(String users,String roleId){
		
		if (StringUtil.isEmpty(users) || StringUtil.isEmpty(roleId)){
			return false; 
		}
		
		String[] userArr = users.split(SPLIT_COMMA);
		return dao.saveRoleUsers(userArr, roleId) > 0;
	}


	/*
	 * (non-Javadoc)
	 * @see com.qph.app.system.service.IRoleService#delRoleUsers(java.lang.String, java.lang.String)
	 */
	public boolean delRoleUsers(String users, String roleId) {
		if (StringUtil.isEmpty(users) || StringUtil.isEmpty(roleId)){
			return false; 
		}
		
		String[] userArr = users.split(SPLIT_COMMA);
		return dao.delRoleUsers(userArr, roleId) > 0;
	}


	
	/*
	 * (non-Javadoc)
	 * @see com.qph.app.system.service.IRoleService#saveRolePerm(java.lang.String, java.lang.String)
	 */
	@Transactional
	public boolean saveRolePerm(String pids, String roleId) {
		
		if (StringUtil.isEmpty(pids) || StringUtil.isEmpty(roleId)){
			return false; 
		}
		
		dao.delRolePerm(roleId);
		
		String[] permArr = pids.split(SPLIT_COMMA);
		
		return dao.saveRolePerms(permArr, roleId) > 0;
	}


	
	/*
	 * (non-Javadoc)
	 * @see com.qph.app.system.service.IRoleService#getPermListByRole(java.lang.String)
	 */
	public List<Permission> getPermListByRole(String roleId) {
		return dao.getPermListByRole(roleId);
	}
	
}
